//
//  PurchasePowerListController.swift
//  SCA
//
//  Created by tjun on 2025/9/12.
//

import UIKit

 

class PurchasePowerListController: BaseController {

    private var datas: [PurchasePowerListData] = []

    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.backgroundColor = AppStyle.color.background
        tableView.rowHeight = UITableView.automaticDimension
        tableView.contentInsetAdjustmentBehavior = .never;
        tableView.register(PurchasePowerListCell.self, forCellReuseIdentifier: PurchasePowerListCell.identifier)
        return tableView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let headerView = UIView(frame: CGRectMake(0, 0, view.width, 100))
        
        let titleLabel = UILabel(text: "智云AI算力".localizedString, font: UIFontSemiboldMake(20), textColor: .white, textAlignment: .center).addTo(headerView)
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(AppStyle.margin.left)
            make.top.equalTo(10)
        }
        
        let stateLabel = UILabel(text: "智云AI是一个结合AI、RWA及DePIN的的全新智能生态。\n\n我们致力于让每一位普通人，都能轻松参与AI产业，透过节点经济、算力共享和生态裂变，实现财富共创、共荣、共赢。".localizedString, font: UIFontMake(12), textColor: .white, textAlignment: .left).addTo(headerView)
        stateLabel.numberOfLines = 0
        stateLabel.attributedText = stateLabel.text?.titleMargin(3, alignment: .left)
        stateLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(AppStyle.margin.left)
            make.top.equalTo(titleLabel.snp.bottom).offset(8)
        }
        
        headerView.setNeedsLayout()
        headerView.layoutIfNeeded()
        
        printf("stateLabel = \(stateLabel)")
        headerView.frame = CGRectMake(0, 0, view.width, stateLabel.maxY + 22)
        
        tableView.tableHeaderView = headerView
        tableView.addTo(view)
        tableView.snp.makeConstraints { make in
            make.top.equalTo(navBarHeight)
            make.left.right.bottom.equalToSuperview()
        }
        
        loadData()

    }
    
    private func loadData() {
        ComputingPowerService.loadPowerListData(success: { models in
            for model in models {
                self.datas.append(PurchasePowerListData(model: model))
            }
            self.tableView.reloadData()
        }) { error in
            
        }
    }
}

extension PurchasePowerListController: UITableViewDataSource, UITableViewDelegate {
 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: PurchasePowerListCell.identifier, for: indexPath) as! PurchasePowerListCell
        let data = datas[indexPath.row]
        cell.data = data
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let data = datas[indexPath.row]
        return data.height
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        let data = datas[indexPath.row]
        let buyPowerController = BuyPowerController(model: data.model)
        navigationController?.pushViewController(buyPowerController, animated: true)
    }
}


